<?php
/*
 * 日程/任务控制器
 */
require 'email.php';
header("Content-Type: text/html;charset=UTF-8");
class Schedule extends CI_Controller {
	function __construct() {
		parent::__construct ();
		$this->load->helper ( 'url' );
	}

	function display($year = null, $month = null) {
		$user_name=$this->session->userdata('username');
		if($user_name){
			$this->load->model ( 'schedule_model','schedule_model' );
			$data ['calendar'] = $this->schedule_model->generate ( $year, $month );
			$data ['page_title'] = '日程/任务 — 昊美科技CRM系统';
			$data ['class5'] = 'cur';
			$this->load->view ( 'schedule_list', $data );
		}else{
			$this->load->view ( 'login');
		}
	}

	function check($day = null,$id = null) {
		$query = $this->db->get_where('schedule',array('schedule_id' => $id));
		$data['find_schedule'] = $query->result();

		/*$startdate=strtotime($day);
		$now_day=date('Y-m-d', strtotime('now'));
		$enddate=strtotime($now_day);
		$interval_days=round(($startdate-$enddate)/3600/24); //时间差

		$data['interval_days'] = array();
		if($interval_days <=30){
			for($i=1;$i<=$interval_days;$i++){
				$data['interval_days'][]=$i;
			}
		}else{
			for($i=1;$i<=30;$i++){
				$data['interval_days'][]=$i;
			}
		}*/
		$query = $this->db->query('SELECT user_id FROM schedule where schedule_id = '.$id.'');
		$row = $query->row_array();
		$data['user_id']=$row['user_id'];
		$this->load->view ( 'schedule_view',$data );
	}

	function delete($id = null){
		$this->db->delete('schedule', array('schedule_id' => $id));
		redirect ( 'schedule/display/' );
	}

	function update_edit(){
		$data_array = $_POST;

		$user_id=$this->session->userdata('id');
		$data_array['user_id']=$user_id;
		$id=$data_array['schedule_id'];
		unset($data_array['schedule_id']);

		$now_time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['update_time']=$now_time;

		$newdate = strtotime($data_array['execution_time']) - (3600 * 24 * $data_array['advance_date']);
		$advance_days = date('Y-m-d',$newdate);
		$data_array['advance_day']=$advance_days;//几号提醒

		$this->db->where('schedule_id', $id);
		$this->db->update('schedule', $data_array);
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'schedule/display/' );
		}else{
		}
	}

	function add($day = null){
		$data['day']=$day;

		/*$startdate=strtotime($day);
		$now_day=date('Y-m-d', strtotime('now'));
		$enddate=strtotime($now_day);
		$interval_days=round(($startdate-$enddate)/3600/24); //时间差

		$data['interval_days'] = array();
		if($interval_days <=30){
			for($i=1;$i<=$interval_days;$i++){
				$data['interval_days'][]=$i;
			}
		}else{
		for($i=1;$i<=30;$i++){
				$data['interval_days'][]=$i;
			}
		}
		<?php for($i=0;$i<count($interval_days);$i++){ ?>
               	    	<?php if(count($interval_days) >= 3){ ?>
               	    		<option value="<?= $interval_days[$i];?>" <?php if($interval_days[$i] == 3){ ?>selected='selected'<?php } ?>><?= $interval_days[$i];?>天</option>
               	    	<?php } ?>
               	        <?php if(count($interval_days) < 3){ ?>
               	    		<option value="<?= $interval_days[$i];?>"><?= $interval_days[$i];?>天</option>
               	    	<?php } ?>
					<?php } ?>
		*
		*/
		$data['show_remind']=0;
		$this->load->view ( 'schedule_add',$data );
	}

	function insert(){
		$user_id=$this->session->userdata('id');
		$data_array = $_POST;

		if($data_array['advance_date'] == null){
			$data_array['advance_date'] = 3;
		}
		if($data_array['show_execution_time'] != null){ /*客户功能模块添加日程*/
			$data_array['execution_time'] = $data_array['show_execution_time'];
			unset($data_array['show_execution_time']);
		}
		$data_array['user_id']=$user_id;
		$now_time=date('Y-m-d H:i:s', strtotime('now'));
		$data_array['create_time']=$now_time;

		$newdate = strtotime($data_array['execution_time']) - (3600 * 24 * $data_array['advance_date']);
		$advance_days = date('Y-m-d',$newdate);
		$data_array['advance_day']=$advance_days;//几号提醒

		$this->db->insert('schedule', $data_array);
		$subject = $data_array['subject'];
		$row=$this->db->affected_rows();
		if($row > 0){
			redirect ( 'schedule/display/' );
		}else{
		}
	}

	public function get_schedule($id = ''){ /*获取某个日程*/
		$query = $this->db->query("SELECT * FROM schedule where schedule_id = $id");
		$data['find_schedule'] = $query->result();
		$this->load->view ( 'schedule_view',$data );
	}

	public function advance_days(){ /*提前多少天邮件通知*/
		$now=date('Y-m-d', strtotime('now'));
		$newdate = strtotime($now) + (3600 * 24 * 3);
		$three_days = date('Y-m-d',$newdate);

		$sql="SELECT * FROM schedule where execution_time = '".$three_days."' and advance_date = 3 and status <> '完成' and status <> '取消'";
		$this->email_content($sql);/*默认提前三天通知*/

		$sql="SELECT * FROM schedule where advance_date <> 3 and status <> '完成' and status <> '取消'";
		$query = $this->db->query($sql);

		foreach ($query->result() as $row){
			$datatime = strtotime($row->execution_time);
			$execution_time = date( "Y-m-d",$datatime);

			$newdate = strtotime($execution_time) - (3600 * 24 * $row->advance_date);
			$advance_time = date('Y-m-d',$newdate);
			if($now  == $advance_time){ /*当日时间等于提前时间*/
				$sql="SELECT * FROM schedule where advance_day = '".$advance_time."' and advance_date <> 3 and status <> '完成' and schedule_id = $row->schedule_id";
				$this->email_content($sql);
			}
		}
	}

	public function email_content($sql = ''){ /*发送邮件的内容*/
		$query = $this->db->query($sql);
		$email=new email();
		foreach ($query->result() as $row){
			/*获取用户邮箱*/
			$user_id = $row->user_id;
			$query = $this->db->query("SELECT * FROM user where id = $user_id");
			foreach ($query->result() as $user_row){
				$email->send_email($user_row->email,$row->subject,$row->content);
			}
		}
	}
}
?>